package com.atguigu.yygh.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.repository.DepartmentRepository;
import com.atguigu.yygh.repository.ScheduleRepository;
import com.atguigu.yygh.service.IDepartmentService;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author kabuda
 * @create 2023-02-26 21:50
 */
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Resource
    private DepartmentRepository departmentRepository;
    @Override
    public void saveDepartment(Map<String, Object> map) {
        String string = JSON.toJSONString(map);
        Department department = JSON.parseObject(string, Department.class);

        Department result=departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        if(result!=null){
            department.setUpdateTime(new Date());
            department.setIsDeleted(result.getIsDeleted());
            department.setCreateTime(result.getCreateTime());
        }else{
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            department.setCreateTime(new Date());
        }

        departmentRepository.save(department);
    }

    @Override
    public Page<Department> getDepartment(String hoscode, Integer page, Integer limit) {
        Department department = new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        Example example=Example.of(department,matcher);
        //mongodb从o页开始计算，所以-1
        Pageable pageable= PageRequest.of(page-1,limit);

        Page all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null){
            departmentRepository.delete(department);
        }
    }

    @Override
    public List<DepartmentVo> getDeptList(String hoscode) {

        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example=Example.of(department);
        List<Department> all = departmentRepository.findAll(example);

        //key为Bigcode，value对应key值一组一组的Department
        Map<String, List<Department>> departmentList1 = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        ArrayList<DepartmentVo> departmentVos1 = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : departmentList1.entrySet()) {

            String key = entry.getKey();
            List<Department> value = entry.getValue();

            //大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(key);
            departmentVo1.setDepname(value.get(0).getBigname());

            //小科室
            ArrayList<DepartmentVo> departmentVos2 = new ArrayList<>();
            for (Department dep : value) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(dep.getDepcode());
                departmentVo2.setDepname(dep.getDepname());
                departmentVos2.add(departmentVo2);
            }
            departmentVo1.setChildren(departmentVos2);

            departmentVos1.add(departmentVo1);
        }

        return departmentVos1;
    }

    @Override
    public Department getDepartmentByCode(String hospcode, String depcode) {
      return   departmentRepository.getDepartmentByHoscodeAndDepcode(hospcode,depcode);
    }




}
